<template>
  <a-spin :spinning="confirmLoading" style="margin: 5%;">
    <j-form-container>
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="店铺名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入店铺名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所在地区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="area">
              <j-dict-select-tag type="list" v-model="model.area" dictCode="sys_area" placeholder="请选择所在地区" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="详细地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder="请输入详细地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="营业时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="openTime">
              <a-input v-model="model.openTime" placeholder="请输入营业时间"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="服务类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceType">
              <j-dict-select-tag type="list" v-model="model.serviceType" dictCode="service_type" placeholder="请选择服务类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入联系方式"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24"> 
            <a-form-model-item label="优惠措施" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <div>
                <a-button type="primary" @click="handleAddDiscount">添加优惠措施</a-button>
                <div style="display:block;line-height:30px;margin-top: 5px;" v-for="(item,index) in discountArr" :key="index"><span>{{item.name+"：面向"+item.area+item.type+item.discount+"折。"+("享受优待方式："+item.way)+(item.remark?("。备注："+item.remark+"。"):'')}}</span><a-button type="link" @click="()=>{discountArr.splice(index,1)}">删除</a-button></div>
              </div>
            </a-form-model-item> 
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-input v-model="model.description" placeholder="请输入优惠简介"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="优惠详情" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="detail">
              <j-editor v-model="model.detail" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="discountType">
              <a-radio-group v-model="model.discountType" name="radioGroup">
                <a-radio value="0">折扣</a-radio>
                <a-radio value="1">减免</a-radio> 
              </a-radio-group>
            </a-form-model-item>
          </a-col> 
          <a-col :span="24">
            <a-form-model-item label="优惠数额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="discount">
              <a-input-number
                v-model="model.discount"
                :min="0"
                :max="model.discountType=='0'?100:10000"
                :formatter="model.discountType=='0'?value => `${value}%`:value => `¥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')"
                :parser="model.discountType=='0'?value => value.replace('%', ''):value => value.replace(/\¥\s?|(,*)/g, '')"
              />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24"> 
            <a-form-model-item label="优先措施" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <div>
                <a-button type="primary" @click="handleAddPriority">添加优先措施</a-button>
                <div style="display:block;line-height:30px;margin-top: 5px;" v-for="(item,index) in priorityArr" :key="index"><span>{{item.name+"：面向"+item.area+item.type+item.content+("。享受优待方式："+item.way)+(item.remark?("。备注："+item.remark+"。"):'')}}</span><a-button type="link" @click="()=>{priorityArr.splice(index,1)}">删除</a-button></div>
              </div>
            </a-form-model-item> 
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优先简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="priorityDesc">
              <a-input v-model="model.priorityDesc" placeholder="请输入优待内容"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="优先详情" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="priorityDetail">
              <j-editor v-model="model.priorityDetail" />
            </a-form-model-item>
          </a-col>  -->
          <a-col :span="24">
            <a-form-model-item label="机构简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="desc">
              <j-editor v-model="model.businessDesc" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="封面" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imageUrl">
              <j-image-upload v-model="model.imageUrl" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="介绍图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imageDesc">
              <j-image-upload v-model="model.imageDesc" :isMultiple="true"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="身份证正面" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imageSfzzm">
              <j-image-upload v-model="model.imageSfzzm" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="身份证反面" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imageSfzfm">
              <j-image-upload v-model="model.imageSfzfm" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="营业执照" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imageYyzz">
              <j-image-upload v-model="model.imageYyzz" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="承诺书" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imageCns">
              <j-image-upload v-model="model.imageCns" ></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <sys-service-discount-modal ref="disCountForm" @ok="disCountOk"></sys-service-discount-modal >
      <sys-service-priority-modal ref="priorityForm" @ok="priorityOk"></sys-service-priority-modal >
    </j-form-container>
    
    <a-button type="primary" @click="submitForm" block>提交</a-button>
  </a-spin>
</template>

<script>

  import { httpAction } from '@/api/manage'
    import SysServiceDiscountModal from './veterans/modules/SysServiceDiscountModal'
  import SysServicePriorityModal from './veterans/modules/SysServicePriorityModal'

  export default {
    name: 'SysServiceAdd',
    components: {
      SysServiceDiscountModal,
      SysServicePriorityModal
    },
    data () {
      return {
        model:{
          name:'',
          area:'',
          address:'',
          openTime:'',
          serviceType:'',
          phone:'',
          description:'',
          detail:'',
          discountType:'0',
          discount:0,
          priorityDesc:'',
          priorityDetail:'',
          businessDesc:'',
          imageDesc:'',
          imageSfzzm:'',
          imageSfzfm:'',
          imageYyzz:'',
          imageCns:''
        },
        discountArr:[],
         priorityArr:[],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name:[{required: true, message: '请输入店铺名称!' }],
          area:[{required: true, message: '请选择所在区域!' }],
          address:[{required: true, message: '请输入具体地址!' }],
          openTime:[{required: true, message: '请输入营业时间!' }],
          serviceType:[{required: true, message: '请选择服务类型!' }],
          phone:[{required: true, message: '请输入联系方式!' }],
          description:[{required: true, message: '请输入优惠简介!' }],
          discount:[{required: true, message: '请输入优惠数额!' }],
          priorityDesc:[{required: true, message: '请输入优先简介!' }],
          priorityDetail:[{required: true, message: '请输入优先详情!' }],
          businessDesc:[{required: true, message: '请输入机构简介!' }],
          detail:[{required: true, message: '请输入优惠详情!' }],
          imageDesc:[{required: true, message: '请上传机构介绍图!' }],
          imageSfzzm:[{required: true, message: '请上传身份证正面!' }],
          imageSfzfm:[{required: true, message: '请上传身份证反面!' }],
          imageYyzz:[{required: true, message: '请上传营业执照!' }],
          imageCns:[{required: true, message: '请上传承诺书!' }]  
        },
        url: {
          add: "/discount/sysService/apply"
        }
      }
    },
    methods: {
      handleAddDiscount(){
        this.$refs.disCountForm.add();
        this.$refs.disCountForm.title = "新增";
        this.$refs.disCountForm.disableSubmit = false;
      },
      handleAddPriority(){
        this.$refs.priorityForm.add();
        this.$refs.priorityForm.title = "新增";
        this.$refs.priorityForm.disableSubmit = false;
      },
      disCountOk(data){
        console.log(data);
        this.discountArr.push(data)
      },
      priorityOk(data){
        console.log(data);
        this.priorityArr.push(data)
      },
      submitForm () {
        console.log("imgs",this.model.imageDesc)
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = this.url.add;
            let method = 'post';
            httpAction(httpurl,{...this.model,discounts:this.discountArr,priorities:this.priorityArr},method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                location. reload();
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false; 
            })
          }
        })
      }
    }
  }
</script>